package com.s7.widget.helper;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.widget.EditText;
import android.widget.TextView;

import com.s7.widget.listener.OnKeyboardListener;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class NumberKeyboardHelper implements KeyboardView.OnKeyboardActionListener {

    /**
     * 空白键
     */
    public static final int KEYCODE_EMPTY = -10;

    private Context mContext;
    private KeyboardView mKeyboard;
    private EditText mEdit;
    private OnKeyboardListener mListener;

    /**
     * adapter 存在的标志
     */
    private boolean hasInit = false;

    public NumberKeyboardHelper(Context mContext) {
        this.mContext = mContext;
    }

    public NumberKeyboardHelper setKeyboardView(KeyboardView mKeyboard) {
        this.mKeyboard = mKeyboard;
        return this;
    }

    public NumberKeyboardHelper setEditText(EditText mEdit) {
        this.mEdit = mEdit;
        return this;
    }

    public NumberKeyboardHelper setOnKeyboardListener(OnKeyboardListener mListener) {
        this.mListener = mListener;
        return this;
    }


    /**
     *
     * @param id res/xml 文件夹下键盘文件
     */
    public void init(int id) {
        if (!hasInit) {
            if (mKeyboard != null) {
                Keyboard keyboard = new Keyboard(mContext, id);
                mKeyboard.setKeyboard(keyboard);
                mKeyboard.setEnabled(true);
                mKeyboard.setPreviewEnabled(false);
                mKeyboard.setOnKeyboardActionListener(this);
            }
            hasInit = true;
        }
    }

    // 0-9 的数字
    private final List<Character> keyCodes = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

    /**
     * 随机打乱数字键盘上显示的数字顺序。
     */
    public void shuffleKeyboard() {
        Keyboard keyboard = mKeyboard.getKeyboard();
        if (keyboard != null && keyboard.getKeys() != null
                && keyboard.getKeys().size() > 0) {
            // 随机排序数字
            Collections.shuffle(keyCodes);

            // 遍历所有的按键
            List<Keyboard.Key> keys = mKeyboard.getKeyboard().getKeys();
            int index = 0;
            for (Keyboard.Key key : keys) {
                // 如果按键是数字
                if (key.codes[0] != KEYCODE_EMPTY
                        && key.codes[0] != Keyboard.KEYCODE_DELETE) {
                    char code = keyCodes.get(index++);
                    key.codes[0] = code;
                    key.label = Character.toString(code);
                }
            }
            // 更新键盘
            mKeyboard.setKeyboard(keyboard);
        }
    }

    @Override
    public void onPress(int primaryCode) {

    }

    @Override
    public void onRelease(int primaryCode) {

    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        if (mEdit != null) {
            Editable editable = mEdit.getText();
            int start = mEdit.getSelectionStart();
            // 处理按键的点击事件
            // 点击删除按键
            if (primaryCode == Keyboard.KEYCODE_DELETE) {
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (primaryCode > 0) {
                // 点击了其他按键
                editable.insert(start, Character.toString((char) primaryCode));
            } else {
                if (mListener != null) {
                    mListener.onKeyEvent(primaryCode);
                }
            }
        }
    }

    /**
     * 将 TextView 内容转换为 Editable 格式
     * @param textView
     * @return
     */
    public Editable getText(TextView textView) {
        CharSequence text = textView.getText();
        if (text == null) {
            return null;
        }
        if (text instanceof Editable) {
            return (Editable) textView.getText();
        }
        textView.setText(text, TextView.BufferType.EDITABLE);
        return (Editable) textView.getText();
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}
